package truedano.truedanostock;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ListAllStock2 extends Util {
	private Button back,update;
	private ListView stockListView;
	String[] updatetime,number,name,price,div,cheap,normal,high,forecast_dividend;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_all_stock2);
		
		setView();
	}
	
	private void setView(){
		final int DBCount = getDBCount();
		updatetime = new String[DBCount];
		number = new String[DBCount];
		name = new String[DBCount];
		price = new String[DBCount];
		div = new String[DBCount];
		cheap = new String[DBCount];
		normal = new String[DBCount];
		high = new String[DBCount];
		updatetime = readDBByName(MyDB.UPDATE_TIME);
		number = readDBByName(MyDB.STOCK_NUMBER);
		name = readDBByName(MyDB.STOCK_NAME);
		price = readDBByName(MyDB.STOCK_PRICE);
		div = readDBByName(MyDB.AVERAGE_DIV);
		cheap = readDBByName(MyDB.CHEAP_PRICE);
		normal = readDBByName(MyDB.NORMAL_PRICE);
		high = readDBByName(MyDB.HIGH_PRICE);
		forecast_dividend = readDBByName(MyDB.FORECAST_DIV);
		
		stockListView = (ListView)findViewById(R.id.listView_stocklist);
		stockListView.setAdapter(stockAdapter);
		stockListView.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				final int click_pos = position;
				CharSequence[] itemsId = {getString(R.string.delete),getString(R.string.update)};
				new AlertDialog.Builder(ListAllStock2.this)
				.setItems(itemsId , new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						if(which == 0){
							deleteDB(number[click_pos]);
							setView();
						}else if(which == 1){
							refreshData(click_pos);
						}
					}
				})
				.show();
				
			}
			
		});
		
		
		back = (Button)findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				closeActivity();
			}
			
		});
		
		update = (Button)findViewById(R.id.update);
		update.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				refreshAllData();
			}
			
		});
	}
	
	BaseAdapter stockAdapter = new BaseAdapter(){

		@Override
		public int getCount() {
			return getDBCount();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final int final_position = position;
			
			convertView = LayoutInflater.from(ListAllStock2.this).inflate(R.layout.list_all_stock_items,null);
			
			TextView update_time = (TextView)convertView.findViewById(R.id.update_time);
			update_time.setText(updatetime[position]);
			
			//TextView stock_number = (TextView)convertView.findViewById(R.id.stock_number);
			//stock_number.setText(number[position]);
			
			TextView stock_price = (TextView)convertView.findViewById(R.id.stock_price);
			stock_price.setText(price[position]);
			
			TextView stock_name = (TextView)convertView.findViewById(R.id.stock_name);
			stock_name.setText(name[position]);
			
			TextView average_div = (TextView)convertView.findViewById(R.id.average_div);
			average_div.setText(div[position]);
			
			TextView cheap_price = (TextView)convertView.findViewById(R.id.cheap_price);
			cheap_price.setText(cheap[position]);
			
			TextView normal_price = (TextView)convertView.findViewById(R.id.normal_price);
			normal_price.setText(normal[position]);
			
			TextView high_price = (TextView)convertView.findViewById(R.id.high_price);
			high_price.setText(high[position]);
			
			TextView forecast_div = (TextView)convertView.findViewById(R.id.forecast_div);
			float show_forecat = Float.parseFloat(forecast_dividend[position])*100;
			forecast_div.setText(show_forecat+"%");
			if(show_forecat >=3.25 && show_forecat < 5){
				forecast_div.setTextSize(20);
				forecast_div.setTextColor(Color.RED);
			}
			else if(show_forecat >=5 && show_forecat < 6.25){
				forecast_div.setTextSize(20);
				forecast_div.setTextColor(Color.GREEN);
			}
			else if(show_forecat >6.25){
				forecast_div.setTextSize(20);
				forecast_div.setTextColor(Color.BLUE);
			}
			
			Button update_item = (Button)convertView.findViewById(R.id.update_item);
			update_item.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					refreshData(final_position);
				}
			});
			
			
			Button delete_item = (Button)convertView.findViewById(R.id.delete_item);
			delete_item.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					deleteDB(number[final_position]);
					setView();
				}
			});
			
			return convertView;
		}
		
	};
	
	private void refreshAllData(){
		if(isInternetExist() == false){
			showToast(getString(R.string.no_internet));
			return;
		}
		
		final int DBCount = getDBCount();
		final String[] number = readDBByName(MyDB.STOCK_NUMBER);
		
		Thread getData = new Thread(){
			@Override
			public void run() {
				for(int i=0;i<DBCount;i++){
					String result = getHtmlResult(number[i]);
					String current_price = getCurrentPrice(result,number[i]);
					String stock_name = getStockName(result,number[i]);
					float div  = getCashDividend(number[i]);
					writeAllDBItems(number[i],current_price,stock_name,div);
				}
				setHandler(H_SET_VIEW,handler);
			}
		};
		getData.start();
		
	}
	
	private void refreshData(final int which){
		if(isInternetExist() == false){
			showToast(getString(R.string.no_internet));
			return;
		}
		final String[] number = readDBByName(MyDB.STOCK_NUMBER);
		Thread getData = new Thread(){
			@Override
			public void run() {
				String result = getHtmlResult(number[which]);
				String current_price = getCurrentPrice(result,number[which]);
				String stock_name = getStockName(result,number[which]);
				float div  = getCashDividend(number[which]);
				deleteDB(number[which]);
				addDB(MyDB.STOCK_NUMBER,number[which]);
				writeAllDBItems(number[which],current_price,stock_name,div);
				setHandler(H_SET_VIEW,handler);
			};
		};
		getData.start();
	}
	
	public static final int H_SET_VIEW     = 0;
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what){
			case H_SET_VIEW:
				setView();
				showToast(getString(R.string.update_success));
				break;
			}
		}
	};
}
